<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary">添加角色</el-button>
                </el-col>
            </el-row>
            <el-table :data="Roleslist" stripe style="width: 100%" border>
            <el-table-column type="expand">
               <template v-slot="scope">
                   <el-row v-for="(item,i1) in scope.row.children" :key="item.id" :class="['el-row',i1==0 ? 'el-top':'','d-center']">
                       <el-col :span="6">
                          <el-tag closable @close="removeRightId(scope.row,item.id)">{{item.authName}}</el-tag>
                          <i class="el-icon-caret-right"></i>
                       </el-col>
                       <el-col :span="18">
                          <el-row v-for="item2 in item.children" :key="item2.id" :class="['d-center']" >
                             <el-col :span='6'>
                                 <el-tag type="success" closable @close="removeRightId(scope.row,item2.id)">{{item2.authName}}</el-tag>
                                 <i class="el-icon-caret-right"></i>
                             </el-col>
                             <el-col :span="18">
                                 <el-tag type="warning" v-for="item3 in item2.children" :key="item3.id" closable @close="removeRightId(scope.row,item3.id)" >
                                     {{item3.authName}}
                                 </el-tag>
                                 <i class="el-icon-caret-right"></i>
                             </el-col>
                          </el-row>
                       </el-col>
                   </el-row>
                  {{scope.row}}
               </template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column prop="roleName" label="角色名称"></el-table-column>
            <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
            <el-table-column label="操作" width="285px">
                <template v-slot="scope">  
                    <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                    <el-button size="mini" type="warning" icon="el-icon-star-off">删除</el-button>
                    <el-button size="mini" type="danger" icon="el-icon-delete" @click="showSetRightDialog(scope.row)" >分配权限</el-button>
                </template>
            </el-table-column>
            </el-table>
        </el-card>
        <el-dialog title="分配权限" :visible.sync="setRightDialogVisible" width="50%">
               <el-tree :data="rightslist" :props="treeProps" show-checkbox node-key="id" 
               default-expand-all :default-expanded-keys="defkeys" ref="treeRef"></el-tree>
               <span slot="footer" class="dialog-footer">
               <el-button @click="setRightDialogVisible = false">取 消</el-button>
               <el-button type="primary"   @click="allotRights">确 定</el-button>
               </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data(){
        return{
          Roleslist:[],
        //  控制分配权限对话框的显示与隐藏
          setRightDialogVisible:false  ,
          //树形结构所有权限数据
          rightslist:[],
          //树形控件的属性绑定对象
          treeProps:{
            label:"authName",
            children:"children"
          },
          //默认选中的节点ID值数组
          defkeys:[],
          //当前即将分配权限的角色ID
          roleID:""
        }
    },
    created(){
        this.getRolesList()
    },
    methods:{
       async getRolesList(){
            const {data:res}=await this.$http.get("roles")
            // console.log(res)
            if(res.meta.status!==200) return
            this.Roleslist=res.data
        },
        async removeRightId(role,rightId){
            // console.log(role.id,rightId)
            const matter=await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).catch(err=>err)
        if(matter!=='confirm') return this.$message.info("已取消删除")
            const{data:res}=await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
            if(res.meta.status!==200) return this.$message.error("删除失败")
            //不用this.getRolesList()会重新整个页面斗刷新要打开，所有不适用
            role.children=res.data
        },
        async showSetRightDialog(role){
        this.roleID=role.id
         const {data:res}=await this.$http.get("rights/tree")
         if(res.meta.status!==200) return this.$message.error("失败")
         this.rightslist=res.data
         console.log(res.data)
         //获取三级节点的id
         this.getLeafkeys(role,this.defkeys)
          this.setRightDialogVisible=true
        },
        //通过递归的形式,获取角色下所有三级权限的id,并保存到defkeys
        getLeafkeys(node,arr){
            //如果不包含children属性，就是三级节点,因为最后一个没有children属性
            if(!node.children){
                return arr.push(node.id)
            }
            //先执行这行命令将传进来的属性遍历出来传到getLeafkeys里，因为传过来正确就不执行
            //所以if要写在前面
            node.children.forEach(item=>{
                this.getLeafkeys(item,arr)
            })
        },
        //点击为角色分配权限
        async allotRights(){
            const keys=[
                ...this.$refs.treeRef.getCheckedKeys(),
                ...this.$refs.treeRef.getHalfCheckedKeys()
            ]
            // console.log(keys)
            const idStr=keys.join(",")
            console.log(idStr)
            const {data:res}=await this.$http.post(`roles/${this.roleID}/rights`,{rids:idStr})
            if(res.meta.status!==200) return this.$message.error("分配失败")
            this.$message.success("分配成功")
            this.getRolesList()
            this.setRightDialogVisible=false
        }
    }
}
</script> 
<style lang="scss" scoped>
.el-tag{
    margin: 7px;
}
.el-row{
    border-bottom: 1px solid #e8e8e8;
}
.el-top{
    border-top: 1px solid #e8e8e8;
}
.d-center{
  display: flex;
  align-items: center;
}
</style>